package com.kly.cashmall.module.profile.presenter;

import com.google.gson.JsonObject;
import com.kly.cashmall.bean.ProfileInfoBean;
import com.kly.cashmall.bean.ProfileJumpBean;
import com.kly.cashmall.framework.mvp.BaseViewPresenter;
import com.kly.cashmall.services.Network;
import com.kly.cashmall.services.Params;
import com.kly.cashmall.utils.function.ToastUtil;
import com.kly.cashmall.widget.Loading.LoadingDialog;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @author
 * @buildBy plugin
 */
public class GeneralInfoPresenter extends BaseViewPresenter<GeneralInfoViewer> {

    public GeneralInfoPresenter(GeneralInfoViewer viewer) {
        super(viewer);
    }

    @Override
    public void willDestroy() {

    }

    public void getPersonInfo(String type) {
        LoadingDialog.showLoading(getActivity());
        Params params = new Params();
        params.put("type", type);
        rxManager.add(Network.getApi().getUserProfile(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<ProfileInfoBean>() {
            @Override
            public void accept(ProfileInfoBean bean) throws Exception {
                LoadingDialog.dismissLoading();
                if (getViewer() != null) {
                    getViewer().getPersoninfoSuccess(bean);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LoadingDialog.dismissLoading();
            }
        }));
    }

    public void savePersonInfo(JsonObject object, String type) {
        LoadingDialog.showLoading(getActivity());
        Params params = new Params();
        params.put("type", type);
        params.put("paramsList", object);
        rxManager.add(Network.getApi().getUserProfileSave(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<ProfileJumpBean>() {
            @Override
            public void accept(ProfileJumpBean bean) throws Exception {
                LoadingDialog.dismissLoading();
                if (getViewer() != null) {
                    getViewer().savePersonInfoSuccess(bean);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LoadingDialog.dismissLoading();
                ToastUtil.showAppToast(throwable.getMessage());
            }
        }));
    }
}